/*
 * Copyright (C) 2007-2012 by XDEV Software, All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 3.0 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 
package xdev.ui;


import java.util.ArrayList;
import java.util.List;


/**
 * Object used to control a validation process and to record the validation
 * exceptions.
 * 
 * @author XDEV Software
 * @since 3.1
 */
public class Validation
{
	private List<ValidationException>	exceptions	= new ArrayList();
	

	/**
	 * Stores a validation exception.
	 * 
	 * @param e
	 *            the occured exception
	 */
	public void record(ValidationException e)
	{
		exceptions.add(e);
	}
	

	/**
	 * Returns <code>true</code> if the validation should be continued after a
	 * exception has occured, <code>false</code> otherwise.
	 * 
	 * @param e
	 *            the occured exception
	 */
	public boolean continueValidation(ValidationException e)
	{
		return true;
	}
	

	/**
	 * Returns all recorded exceptions of this validation process.
	 * 
	 * @return all recorded exceptions
	 */
	public ValidationException[] getExceptions()
	{
		return exceptions.toArray(new ValidationException[exceptions.size()]);
	}
	

	/**
	 * Returns all exceptions with a specific severity of this validation
	 * process.
	 * 
	 * @param severity
	 * @return all exceptions with a specific severity
	 */
	public ValidationException[] getExceptionsOf(Severity severity)
	{
		List<ValidationException> list = new ArrayList();
		for(ValidationException e : exceptions)
		{
			if(e.getSeverity() == severity)
			{
				list.add(e);
			}
		}
		return list.toArray(new ValidationException[list.size()]);
	}
	

	/**
	 * Returns all exceptions with a minimum severity of this validation
	 * process.
	 * 
	 * @param minSeverity
	 * @return all exceptions with a minimum severity
	 */
	public ValidationException[] getExceptiosFrom(Severity minSeverity)
	{
		List<ValidationException> list = new ArrayList();
		for(ValidationException e : exceptions)
		{
			if(e.getSeverity().ordinal() >= minSeverity.ordinal())
			{
				list.add(e);
			}
		}
		return list.toArray(new ValidationException[list.size()]);
	}
	

	/**
	 * Returns <code>true</code> if at least one of the recorded exceptions is
	 * an error.
	 * 
	 * @see ValidationException#getSeverity()
	 */
	public boolean hasError()
	{
		for(ValidationException e : exceptions)
		{
			if(e.getSeverity() == Severity.ERROR)
			{
				return true;
			}
		}
		
		return false;
	}
}
